# %%
from lxml import etree
from requests import request, get
import os

# %%
root_path = "./modules/"   # 要存放模块文件的位置

request_url = "https://www.jamovi.org/library.html"
request_header = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.39",
}

respoense = request(method="get", url=request_url, headers=request_header)
respoense.encoding="utf-8"
data = etree.HTML(respoense.text)
module_cards = data.xpath('//*[@class="jmv-store-module-rhs"]')

for each_card in module_cards:
    module_title = each_card.xpath("./h2/text()")    # 模块的名称
    module_edition = each_card.xpath('./h2/span[@class="version"]/text()')    # 模块的版本号
    module_description = each_card.xpath('./div[@class="description"]/text()')    # 模块的描述

    

    module_download_links = each_card.xpath('./a')
    for each_download_link in module_download_links:
        link = each_download_link.xpath("./@href")[0]  # 模块的下载地址
        link_type = each_download_link.xpath('./button[@class="jmv-store-module-button"]/span[@class="label"]/text()')[0]  # 模块适用的系统 Windows Linux MacOs
        
        url_file_name = link.split('/')[-1]    # 获取url中的文件名(因为这个文件名是合法的文件名)
        url_file_name = url_file_name.replace(".jmo", "")   # 去掉文件名后缀后备用
        
        file_path = os.path.join(root_path, url_file_name)    # 合成下载路径
        file_name = f"{url_file_name}-{link_type}.jmo"    # 生成下载文件的文件名中
        

        if not os.path.exists(file_path):    # 如果下载路径不存在，就新建一个
            os.makedirs(file_path)
            with open(os.path.join(file_path,"description.txt"), "a+") as des_file:
                des_file.write(f"Name:{module_title[0].strip()}\n")
                des_file.write(f"Edition:{module_edition[0].strip()}\n")
                des_file.write(f"Description:{module_description[0].strip()}")
        
        if os.path.isfile(os.path.join(file_path,file_name)):    # 如果这个模块已经被下载下来了，就跳过这个模块
            continue

        r = get(link, stream = True)   # 从链接下载文件
        with open(os.path.join(file_path,file_name), "wb") as module_file:
            for chunk in r.iter_content(chunk_size = 1024): # 1024 bytes
                if chunk:
                    module_file.write(chunk)



